/*

This file is part of Rooms3D.

Copyright (C) 2008 Pixecur Technologies Incorporated (Esotera).
Visit Esotera at http://www.esoteras3.com
 
Rooms3D is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option)
any later version.
 
Rooms3D is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
General Public License for more details.
 
You should have received a copy of the GNU General Public License
along with Rooms3D; see the file COPYING.  If not, write to 
Pixecur Technologies, 213-31 Peet Street, St. John's, NL, A1B 3W8. 
 
Linking this library statically or dynamically with other modules is
making a combined work based on this library.  Thus, the terms and
conditions of the GNU General Public License cover the whole
combination.
 
*/

package com.datasentinel.j3d.controls;

import java.awt.Color;
import java.awt.Font;
import java.awt.SystemColor;

import javax.media.j3d.Appearance;
import javax.media.j3d.Material;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.Color3f;
import javax.vecmath.Vector3d;
import javax.vecmath.Vector3f;

import com.datasentinel.j3d.utils.geometry.MutableText2D;
import com.sun.j3d.utils.geometry.Cylinder;

public class J3DProgressBarUI extends J3DComponentUI
{
	
	private Font textFont;
	private Color frameColor;
	private Color progressBarColor;
	private Color progressTextColor;
	private float progressBarRadius;

	/**
	 * -- Branch elements --
	 * 
	 * Internal primitive disposition
	 */

	private Transform3D translateTextT3D;
	private Transform3D translateProgressT3D;
	private Transform3D scaleProgressT3D;
	private Transform3D translateFrameT3D;
	//private Transform3D scaleFrameT3D;
	private Transform3D rotateFrameT3D;

	private TransformGroup translateTextTrnsGrp;
	private TransformGroup translateProgressTrnsGrp;
	private TransformGroup scaleProgressTrnsGrp;
	private TransformGroup translateFrameTrnsGrp;
//	private TransformGroup scaleFrameTrnsGrp;
	private TransformGroup rotateFrameTrnsGrp;
	
	/**
	 * Appearance and material properties
	 */
	private Material frameMaterial;
	private Material progressBarMaterial;
	
	private Appearance frameAppearance;
	private Appearance progressBarAppearance;
	
	/**
	 * Visual primitives
	 */
	private Cylinder frameCylinder;
	private Cylinder progressBarCylinder;
	private MutableText2D progressText2D;

	

	
	public J3DProgressBarUI()
	{
	}
	

	
	
	@Override
	protected void initializeComponentParts()
	{
		

		frameColor = SystemColor.controlLtHighlight;
		progressBarColor = Color.GREEN.darker().darker();
		progressTextColor = Color.BLACK;
		progressBarRadius = 0.05f;
		
		textFont = new Font(Font.SANS_SERIF, Font.BOLD, 17);
		
		
		
		frameCylinder = new Cylinder(progressBarRadius, 1.0f);
		progressBarCylinder = new Cylinder(progressBarRadius, 1.0f);
		progressText2D = new MutableText2D("", new Color3f(progressTextColor),
				textFont.getFontName(), textFont.getSize(), textFont.getStyle());
		
		
		frameMaterial = new Material();
		frameMaterial.setDiffuseColor(new Color3f(frameColor));
//		frameMaterial.setLightingEnable(true);
		
		TransparencyAttributes frameTransparencyAttributes;
		frameTransparencyAttributes = new TransparencyAttributes();
		frameTransparencyAttributes.setTransparencyMode(TransparencyAttributes.FASTEST);
//		frameTransparencyAttributes.setDstBlendFunction(TransparencyAttributes.BLEND_SRC_ALPHA);
		frameTransparencyAttributes.setTransparency(0.8f);
		
		frameAppearance = new Appearance();
		frameAppearance.setMaterial(frameMaterial);
		frameAppearance.setTransparencyAttributes(frameTransparencyAttributes);
		
		frameCylinder.setAppearance(frameAppearance);
		
		
		progressBarMaterial = new Material();
		progressBarMaterial.setDiffuseColor(new Color3f(progressBarColor));
		
		progressBarAppearance = new Appearance();
		progressBarAppearance.setMaterial(progressBarMaterial);
		
		progressBarCylinder.setAppearance(progressBarAppearance);
		
	}

	@Override
	protected void initializeComponentPartsLayout()
	{
		translateTextT3D = new Transform3D();
		translateProgressT3D = new Transform3D();
		scaleProgressT3D = new Transform3D();
		translateFrameT3D = new Transform3D();
		//scaleFrameT3D = new Transform3D();
		rotateFrameT3D = new Transform3D();
		
		translateFrameT3D.setTranslation(new Vector3f(0.0f, 0.0f, frameCylinder.getRadius()));
		
		rotateFrameT3D.rotZ(Math.PI/2.0);
		
//		Transform3D tempTransform = new Transform3D();
//		tempTransform.rotY(Math.PI/ 2.0);
//		rotateFrameT3D.mul(tempTransform);
		
		translateTextTrnsGrp = new TransformGroup(translateTextT3D);
		translateTextTrnsGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);

		translateProgressTrnsGrp = new TransformGroup(translateProgressT3D);
		translateProgressTrnsGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		translateProgressTrnsGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		
		
		scaleProgressTrnsGrp = new TransformGroup(scaleProgressT3D);
		scaleProgressTrnsGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		scaleProgressTrnsGrp.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
		
		
		translateFrameTrnsGrp = new TransformGroup(translateFrameT3D);
//		scaleFrameTrnsGrp = new TransformGroup(scaleFrameT3D);
		rotateFrameTrnsGrp = new TransformGroup(rotateFrameT3D);
		
//		adjustProgressBar(0.0);
		
	}
	
	@Override
	protected void constructComponentGUIBranch()
	{
		/**
		 * >> Level 4
		 */
		scaleProgressTrnsGrp.addChild(progressBarCylinder);
		/**
		 * >> Level 3
		 */
		translateProgressTrnsGrp.addChild(scaleProgressTrnsGrp);
		/**
		 * >> Level 2
		 */
		rotateFrameTrnsGrp.addChild(translateProgressTrnsGrp);
		rotateFrameTrnsGrp.addChild(frameCylinder);
		translateTextTrnsGrp.addChild(progressText2D);
		/**
		 * >> Level 1
		 */
		translateFrameTrnsGrp.addChild(translateTextTrnsGrp);
		translateFrameTrnsGrp.addChild(rotateFrameTrnsGrp);
		/**
		 * >> Level 0
		 */
		baseBrnchGrp.addChild(translateFrameTrnsGrp);
		
		baseBrnchGrp.compile();
	}
	
	private void adjustProgressBar(double scaleFactor)
	{
//		double scaleFactor = 0.5;
		float progressCylinderHeight = progressBarCylinder.getHeight();
		translateProgressT3D.setTranslation(new Vector3d(0.0, -scaleFactor * progressCylinderHeight / 2.0 + progressCylinderHeight / 2.0, 0.0));
		scaleProgressT3D.setScale(new Vector3d(1.0, scaleFactor, 1.0));
		
		translateProgressTrnsGrp.setTransform(translateProgressT3D);
		scaleProgressTrnsGrp.setTransform(scaleProgressT3D);
	}
	
	
	@Override
	public void render3DComponent(J3DComponent j3dComponent)
	{
		J3DProgressBar j3dPRogressBar = (J3DProgressBar)j3dComponent;
		
		ProgressBarModel3D model = j3dPRogressBar.getModel();
		
		progressText2D.setString(model.getText());
		J3DUIHelper.prepareCenteredText2DTransform(j3dPRogressBar.getCanvas3D(), 
				textFont, progressText2D, translateTextT3D, 2.0f * frameCylinder.getRadius());
		translateTextTrnsGrp.setTransform(translateTextT3D);
		
		double scaleFactor = (((double)model.getValue()) - ((double)model.getMinimum())) /
			(((double)model.getMaximum()) - ((double)model.getMinimum())); 
		
		adjustProgressBar(scaleFactor);
	}
	
}
